package com.frxuexi.shop2307.shop

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.SearchView.OnQueryTextListener
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.room.SearchEntity
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.ShopAdapter
import com.frxuexi.shop2307.databinding.ActivitySearchBinding
import com.frxuexi.shop2307.databinding.ItemSearchBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/SearchActivity")
@AndroidEntryPoint
class SearchActivity : BaseActivity<ActivitySearchBinding,ShopViewModel>() {
    val shopAdapter by lazy { ShopAdapter() }
    val historys= mutableListOf<SearchEntity>()
    var ascPrice=false
    var ascSales=false
    override fun initData() {
        vm.shopKeyData.observe(this){
            shopAdapter.apply {
                data.clear()
                addData(it.list)
                notifyDataSetChanged()
            }
        }

        vm.addSearchData.observe(this){
        }

        vm.searchData.observe(this){
            historys.clear()
            historys.addAll(it)
            binding.searchFlex.removeAllViews()
            it.forEach {entity->

                val searchBinding = ItemSearchBinding.inflate(LayoutInflater.from(this))
                searchBinding.searchHistory.setText(entity.key)

                searchBinding.searchHistory.setOnClickListener {
                    binding.searchSv.setQuery(entity.key,true)
                }

                searchBinding.searchDelete.setOnClickListener {
                    vm.deleteSearch(entity)
                }

                binding.searchFlex.addView(searchBinding.root)
            }
        }

        vm.deleteSearchData.observe(this){
            vm.getSearchs()
        }
    }

    override fun initView() {

        binding.searchRv.apply {
            adapter=shopAdapter
            layoutManager=GridLayoutManager(this@SearchActivity,2)
        }
        vm.getShopByKey(0,"",10,1)

        vm.getSearchs()
        binding.searchSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.getShopByKey(0,query!!,10,1)
                vm.addSearch(SearchEntity(key = query!!))
                vm.getSearchs()
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                vm.getShopByKey(0,"",10,1)
                return true
            }
        })

        binding.searchClear.setOnClickListener {
            historys.forEach {
                vm.deleteSearch(it)
            }
        }

        binding.searchConPrice.setOnClickListener {
            if(ascPrice){//升序
                shopAdapter.data.sortBy { it.price.toDouble() }
                binding.searchPriceIv.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_asc)
            }else{
                shopAdapter.data.sortByDescending { it.price.toDouble() }
                binding.searchPriceIv.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_desc)
            }
            shopAdapter.notifyDataSetChanged()
            ascPrice=!ascPrice
        }

        binding.searchConStock.setOnClickListener {
            if(ascSales){//升序
                shopAdapter.data.sortBy { it.sales }
                binding.searchStockIv.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_asc)
            }else{
                shopAdapter.data.sortByDescending { it.sales }
                binding.searchStockIv.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_desc)
            }
            shopAdapter.notifyDataSetChanged()
            ascSales=!ascSales
        }

    }

}